package com.fz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by webrx on 2017-09-05.
 */
@Controller
@RequestMapping("/user")
public class Hello {
    @RequestMapping("/show")  //http://localhost:8080/user/show
    public void show(){
        System.out.println("hello");  //试图渲染显示，会找user/show.jsp
    }
   /* @RequestMapping("")     //http://localhost:8080/user/
    public void aa(HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out=resp.getWriter();
        out.print("wangbadan");
        out.flush();
        out.close();
    }*/
@RequestMapping(value = "sh.do")  //http://localhost:8080/user/sh.do
    public void bb(PrintWriter out){
        out.print("中国123");
    }
@RequestMapping(value = {"cc","ai","c.action"}) @ResponseBody
//http://localhost:8080/user/c.action    http://localhost:8080/user/cc   http://localhost:8080/user/ai
    public String cc(){
        return "cc";
    }

@RequestMapping(value = "/zhang",method = RequestMethod.POST)
//当没有,method = RequestMethod.POST这个的时候，默认提交方式是get，可以在地址栏任意输入，页面显示地址栏输入的内容
//例如http://localhost:8080/user/zhang?name=余壮壮
//当加了这个method = RequestMethod.POST之后就需要从一个jsp页面通过post的方式来提交name了
public void ee(HttpServletResponse resp, HttpServletRequest req) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset=\"utf-8\">");
        out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
        out.println("<meta name=\"keywords\" content=\"关键字\">");
        out.println("<meta name=\"description\" content=\"简介\">");
        out.println("</head>");
        out.println("<body>");

        out.println("<h3>Hello World  中文效果</h3>");
        out.print(req.getParameter("name"));
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();

    }
    @RequestMapping("/ff")    //http://localhost:8080/user/ff
    public String ff(Model m){
        m.addAttribute("name","张三");
        return "ff";
    }
@RequestMapping("/gg")
    public ModelAndView gg(){
        ModelAndView mv=new ModelAndView("aa");    //寻找试图
        mv.addObject("name","张三");
        mv.addObject("address","河南");
        mv.setViewName("user/save");   //找这个试图views/user/save.jsp
       // mv.setViewName("ff");    //这个示例意思是会找到这个视图
        return mv;
    }

    @RequestMapping("/hh")
    public void hh(String name,PrintWriter out){
        out.print(name);
    }

    @RequestMapping(value = "/sss",method = RequestMethod.POST) @ResponseBody    //http://localhost:8080/user/sss?name=lisi&age=18&address=厦门
    public void sss(String name,int age,String address){
        System.out.printf("%s,%d,%s",name,age,address);
    }

    @RequestMapping(value = "/add/{id}/{name}",method = RequestMethod.GET)    //http://localhost:8080/user/add/10/张三.zhuang  "."后带啥都可以只是起到一个迷惑的作用!
    public String add(@PathVariable("id") int sid,@PathVariable("name") String name,Model model){
        model.addAttribute("student_id",sid);
        model.addAttribute("student_name","name");
        return "user/addd";   //这里要找的是这个路径/WEB-INF/views/addd.jsp
    }
}
